package cn.net.dckj.system.service.impl;

import cn.net.dckj.common.core.impl.GenericBizServiceImpl;
import cn.net.dckj.common.core.text.Convert;
import cn.net.dckj.common.utils.DateUtils;
import cn.net.dckj.system.domain.SysAttachment;
import cn.net.dckj.system.mapper.SysAttachmentMapper;
import cn.net.dckj.system.service.ISysAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统附件Service业务层处理
 *
 * @author dckj
 * @date 2019-10-15
 */
@Service
public class SysAttachmentServiceImpl extends GenericBizServiceImpl<SysAttachmentMapper, SysAttachment> implements ISysAttachmentService {
    @Autowired
    private SysAttachmentMapper sysAttachmentMapper;

    /**
     * 查询系统附件
     *
     * @param id 系统附件ID
     * @return 系统附件
     */
    @Override
    public SysAttachment selectSysAttachmentById(Long id) {
        return sysAttachmentMapper.selectSysAttachmentById(id);
    }

    /**
     * 查询系统附件列表
     *
     * @param sysAttachment 系统附件
     * @return 系统附件
     */
    @Override
    public List<SysAttachment> selectSysAttachmentList(SysAttachment sysAttachment) {
        return sysAttachmentMapper.selectSysAttachmentList(sysAttachment);
    }

    /**
     * 新增系统附件
     *
     * @param sysAttachment 系统附件
     * @return 结果
     */
    @Override
    public int insertSysAttachment(SysAttachment sysAttachment) {
        sysAttachment.setCreateTime(DateUtils.getNowDate());
        return sysAttachmentMapper.insertSysAttachment(sysAttachment);
    }

    /**
     * 删除系统附件对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysAttachmentByIds(String ids) {
        return sysAttachmentMapper.deleteSysAttachmentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除系统附件信息
     *
     * @param id 系统附件ID
     * @return 结果
     */
    @Override
    public int deleteSysAttachmentById(Long id) {
        return sysAttachmentMapper.deleteSysAttachmentById(id);
    }

    /**
     * 后台使用校验数据方法
     *
     * @param entity 需要校验的实体
     * @return isNotEmpty返回error信息
     */
    @Override
    public String validate(SysAttachment entity) {
        StringBuilder stringBuilder = new StringBuilder();
        return stringBuilder.toString();
    }

    /**
     * 据自定义查询验证是否存在这个数(excel导入、后台数据验证使用)
     * 需要自己根据业务实现，规则：若数据唯一性一个（如表中仅代码唯一），则返回查询的数据id;
     * 若数据唯一性多个（如表中代码唯一，名称也必须唯一），则直接返回某某数据存在提示，记录于stringBuilder中
     * 这时返回值不需要给定，使用默认返回0L，调用使用该方法会根据stringBuilder是否为空判断是否存在数据
     *
     * @param entity        需要验证数据
     * @param stringBuilder 记录数据是否存在信息
     * @return 数据唯一性一个返回查询实体id，要求id>0，若id<1查询无记录；
     * 若数据唯一性多个，默认返回0L，根据stringBuilder是否空判断是否查询存在数据
     */
    @Override
    public Long checkUnique(SysAttachment entity, StringBuilder stringBuilder) {
        // 设置查询条件
        /*Long id = entity.getId();
        SysAttachment query = new SysAttachment();
        query.setId(id);
        SysAttachment rtn = this.dao.checkUnique(query);
        if (rtn != null && rtn.getId() != null && rtn.getId() > 0) {
            stringBuilder.append("xxxx已经存在!");
            return rtn.getId();
        }*/
        return 0L;
    }

    @Override
    public List<SysAttachment> selectListToMobile(SysAttachment entity) {
        return this.dao.selectSysAttachmentList(entity);
    }

    @Override
    public List<SysAttachment> findByMainId(Long mainId, String entityName) {
        SysAttachment query = new SysAttachment();
        query.setMainId(mainId);
        query.setEntityName(entityName);
        return this.dao.selectAttachmentList(query);
    }
}
